//
//  ZYVisitorView.swift
//  WeiBo
//
//  Created by zhangyu on 16/9/21.
//  Copyright © 2016年 zhangyu. All rights reserved.
//

import UIKit

class ZYVisitorView: UIView {
    //  需要传入执行的闭包
    var loginClosure: (() -> ())?
    //旋转视图
    private lazy var cycleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    //主页
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //未登录信息视图
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        //多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //登录按钮
    private lazy var loginButton:UIButton = {
        let button = UIButton()
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        //添加点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        //去掉点击高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //添加点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    override init (frame: CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        //添加子控件
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        //使用系统自动布局，循环照片
        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        maskImageView.translatesAutoresizingMaskIntoConstraints = false
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        //设置布局方式
        addConstraint(NSLayoutConstraint(item: cycleImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        //罩层布局
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .centerX, relatedBy: .equal, toItem: cycleImageView, attribute: .centerX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: .centerY, relatedBy: .equal, toItem: cycleImageView, attribute: .centerY, multiplier: 1, constant: 0))
        //主页视图布局
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .centerX, relatedBy: .equal, toItem: cycleImageView, attribute: .centerX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .centerY, relatedBy: .equal, toItem: cycleImageView, attribute: .centerY, multiplier: 1, constant: 0))
        //未登录信息视图
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: cycleImageView, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: cycleImageView, attribute: .bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 224))
        //  注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .leading, relatedBy: .equal, toItem: messageLabel, attribute: .leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
        //登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .trailing, relatedBy: .equal, toItem: messageLabel, attribute: .trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: registerButton, attribute: .top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
        
        
        
    }
    //  修饰访客视图信息
    func updateVisitorInfo(message: String?, imageName: String?) {
        
        if let msg = message, let imgName = imageName {
            //  如果代码执行到此,表示消息,发现,我的视图控制器
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imgName)
            //  隐藏 swift 3.0改成isHidden
            cycleImageView.isHidden = true
        } else {
            //  代码执行到此,表示首页
            //  执行旋转动画
            startAnimation()
        }
    }
    private func startAnimation() {
        let  animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        //失去焦点不让其释放
        animation.isRemovedOnCompletion = false
        cycleImageView.layer.add(animation, forKey: nil)
        
    }
    //  MARK: -- 点击事件
    @objc private func loginAction() {
        
        print("我是访客视图点击的操作")
        //  执行闭包
        loginClosure?()
        
    }
    
}
